/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jifAdministrarProveedorSuministro.java
 *
 * Created on 13-dic-2010, 9:09:41
 */

package Suministros;

import administracion.delegate.GestionPedidosDelegate;
import administracion.helper.Listas;
import administracion.helper.Utiles_MensajesDialogo;
import com.jidesoft.hints.ListDataIntelliHints;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;
import suministros.ejb30.TO.ProveedorTO;
import suministros.ejb30.TO.SuministroTO;
import suministros.ejb30.entity.Proveedor;
import suministros.ejb30.entity.ProveedorSuministro;
import suministros.ejb30.entity.ProveedorSuministroPK;
import suministros.ejb30.helper.Utiles_Convertir;

/**
 *
 * @author Diana
 */
public class jifAdministrarProveedorSuministro extends javax.swing.JInternalFrame {
     private ListDataIntelliHints intelliHintsSuministros;
     private ListDataIntelliHints intelliHintsProveedor;
     SuministroTO suministro;
     Proveedor proveedor;
     DefaultFormatterFactory currFactory;
    /** Creates new form jifAdministrarProveedorSuministro */
    public jifAdministrarProveedorSuministro() {
  
        initComponents();
        cargaHintsIndependientes();
    }
    private void construirMascaras() {
    NumberFormat editFormat = NumberFormat.getNumberInstance(Locale.ENGLISH);

    editFormat.setGroupingUsed(false);

    NumberFormatter enFormat = new NumberFormatter(editFormat);

    this.currFactory = new DefaultFormatterFactory(enFormat, enFormat, enFormat);

    enFormat.setAllowsInvalid(true);
  }
 private void cargaHintsIndependientes() {
    this.intelliHintsSuministros = new ListDataIntelliHints(this.jtfDescSuministro, Listas.getSuministros());
    this.intelliHintsProveedor = new ListDataIntelliHints(this.jtfProveedor, Listas.getProveedores());
  }

 private void buscarSuministros()
  {
    this.suministro = null;
    SuministroTO sumi;
    if (this.jtfCodSuministro.getText().trim().length() < 1) {

      try { sumi = (SuministroTO)this.intelliHintsSuministros.getSelectedHint();
      } catch (NullPointerException ex) {
        sumi = null;
      }
    } else {
      sumi = Listas.getSuministroCodigo(this.jtfCodSuministro.getText().trim());
    }

    if (sumi == null) {
      Utiles_MensajesDialogo.mensajeError("El suministro no existe!");
      this.jtfDescSuministro.setText("");
      this.jbAceptar.setEnabled(false);
     } else {
      this.jtfDescSuministro.setText(sumi.toString());
      this.jtfCodSuministro.setText(sumi.getId_suministro());
      this.jbAceptar.setEnabled(true);
      this.jftfCantidad.requestFocus();
      this.suministro = sumi;
      this.buscar();
    }
  }
  private void buscarRazonSocial()
      {
        ProveedorTO pro;
        if (this.jtfRuc.getText().trim().length() < 1) {
              try { pro = (ProveedorTO)this.intelliHintsProveedor.getSelectedHint();
              } catch (NullPointerException ex) {
                pro = null;
              }
            }
        else {
          pro = Listas.getProveedorcodigo(this.jtfRuc.getText().trim());
        }
        if (pro == null) {
          Utiles_MensajesDialogo.mensajeError("El proveedor no existe!");
          this.jtfRuc.setText("");
          pro = null;
        }
        else {
          this.proveedor = Utiles_Convertir.convertirToProveedor_ProveedorTO(pro);
          this.jtfRuc.setText(pro.getRazon() + " - " + pro.getCodProveedor());
          this.jtfRuc.setText(pro.getCodProveedor());

        }

      }
  public void buscar(){
      String pro= this.jtfRuc.getText().toString();
      String sumi=this.jtfCodSuministro.getText().toString();
       ProveedorSuministro prosumi= new ProveedorSuministro();
       ProveedorSuministro prosumi2= new ProveedorSuministro();
       ProveedorSuministroPK prosumipk= new ProveedorSuministroPK(pro, sumi);
       prosumi.setProveedorSuministroPK(prosumipk);
       BigDecimal precio=BigDecimal.ZERO;
        try {
             prosumi2=GestionPedidosDelegate.getInstance().getDatosProveedorSuministro(prosumi);
        } catch (Exception ex) {
            Logger.getLogger(jifAdministrarProveedorSuministro.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(prosumi2!=null){
            precio=prosumi2.getPrecio();
        }
        else{
            precio=BigDecimal.ZERO;
        }
       this.jftfCantidad.setText(precio+"");
  }
  public void guardar(){
       String pro= this.jtfRuc.getText().toString();
       String sumi=this.jtfCodSuministro.getText().toString();
       ProveedorSuministro prosumi= new ProveedorSuministro();
       ProveedorSuministroPK prosumipk= new ProveedorSuministroPK(pro, sumi);
       prosumi.setProveedorSuministroPK(prosumipk);
       prosumi.setPrecio(new BigDecimal(this.jftfCantidad.getText()));
        try {
            GestionPedidosDelegate.getInstance().registrarProveedorSuministro(prosumi);
             Utiles_MensajesDialogo.mensajeExito();
        } catch (Exception ex) {
            Utiles_MensajesDialogo.mensajeError("No se pudo realizar la operacion");
            Logger.getLogger(jifAdministrarProveedorSuministro.class.getName()).log(Level.SEVERE, null, ex);
        }
  }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblProveedor = new javax.swing.JLabel();
        jtfRuc = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jtfProveedor = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jtfCodSuministro = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jtfDescSuministro = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jftfCantidad = new javax.swing.JFormattedTextField();
        jbAceptar = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();

        setTitle("ADMINISTRAR PRECIO SUMINISTROS PROVEEDOR");

        lblProveedor.setFont(new java.awt.Font("Arial", 1, 12));
        lblProveedor.setText("RUC PROVEEDOR");

        jtfRuc.setFont(new java.awt.Font("Arial", 0, 12));
        jtfRuc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfRucActionPerformed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jLabel8.setText("PROVEEDOR:");

        jtfProveedor.setFont(new java.awt.Font("Arial", 0, 12));
        jtfProveedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfProveedorActionPerformed(evt);
            }
        });
        jtfProveedor.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfProveedorKeyPressed(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel9.setText("COD.SUMINISTRO");

        jtfCodSuministro.setFont(new java.awt.Font("Arial 12 Simple 12 Simple", 0, 12));
        jtfCodSuministro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfCodSuministroActionPerformed(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel10.setText("DESC.SUMINISTRO");

        jtfDescSuministro.setFont(new java.awt.Font("Arial 12 Simple 12 Simple", 0, 12));
        jtfDescSuministro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfDescSuministroActionPerformed(evt);
            }
        });
        jtfDescSuministro.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfDescSuministroKeyPressed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel1.setText("PRECIO");

        jftfCantidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jftfCantidadActionPerformed(evt);
            }
        });
        jftfCantidad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jftfCantidadKeyPressed(evt);
            }
        });

        jbAceptar.setFont(new java.awt.Font("Arial", 1, 12));
        jbAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_ok.png"))); // NOI18N
        jbAceptar.setMnemonic('A');
        jbAceptar.setText("ACEPTAR");
        jbAceptar.setToolTipText("Registrar el pedido");
        jbAceptar.setEnabled(false);
        jbAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAceptarActionPerformed(evt);
            }
        });
        jbAceptar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbAceptarKeyPressed(evt);
            }
        });

        jbCancelar.setFont(new java.awt.Font("Arial", 1, 12));
        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jbCancelar.setMnemonic('C');
        jbCancelar.setText("CERRAR");
        jbCancelar.setToolTipText("Cancelar la Operacion");
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });
        jbCancelar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbCancelarKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(lblProveedor)
                                    .addGap(21, 21, 21)
                                    .addComponent(jtfRuc, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(jLabel8)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jtfProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel9)
                                    .addGap(20, 20, 20)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jftfCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jtfCodSuministro, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGap(18, 18, 18)
                                            .addComponent(jLabel10)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(jtfDescSuministro, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)))))
                            .addContainerGap(31, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addContainerGap(608, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jbAceptar)
                        .addGap(18, 18, 18)
                        .addComponent(jbCancelar)
                        .addGap(210, 210, 210))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblProveedor)
                    .addComponent(jLabel8)
                    .addComponent(jtfProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfRuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jtfCodSuministro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel9))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel10)
                        .addComponent(jtfDescSuministro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jftfCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbAceptar)
                    .addComponent(jbCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtfRucActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfRucActionPerformed
        if (this.jtfRuc.getText().trim().length() < 1)
            this.jtfProveedor.requestFocus();
        else{
            buscarRazonSocial();
        }
    }//GEN-LAST:event_jtfRucActionPerformed

    private void jtfProveedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfProveedorActionPerformed
        buscarRazonSocial();
}//GEN-LAST:event_jtfProveedorActionPerformed

    private void jtfProveedorKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfProveedorKeyPressed
        if (this.jtfRuc.getText().trim().length() > 0)
            this.jtfRuc.setText("");
}//GEN-LAST:event_jtfProveedorKeyPressed

    private void jtfCodSuministroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfCodSuministroActionPerformed

        if (this.jtfCodSuministro.getText().trim().length() < 1)
            this.jtfDescSuministro.requestFocus();
        else
            buscarSuministros();
}//GEN-LAST:event_jtfCodSuministroActionPerformed

    private void jtfDescSuministroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfDescSuministroActionPerformed

        buscarSuministros();
}//GEN-LAST:event_jtfDescSuministroActionPerformed

    private void jtfDescSuministroKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfDescSuministroKeyPressed

        if (this.jtfCodSuministro.getText().trim().length() > 0)
            this.jtfCodSuministro.setText("");
}//GEN-LAST:event_jtfDescSuministroKeyPressed

    private void jftfCantidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jftfCantidadActionPerformed

}//GEN-LAST:event_jftfCantidadActionPerformed

    private void jftfCantidadKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jftfCantidadKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jbAceptar.requestFocus();
        }
}//GEN-LAST:event_jftfCantidadKeyPressed

    private void jbAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAceptarActionPerformed
    this.guardar();

}//GEN-LAST:event_jbAceptarActionPerformed

    private void jbAceptarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbAceptarKeyPressed

        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jbAceptar.doClick();
        }
}//GEN-LAST:event_jbAceptarKeyPressed

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed
      int res = javax.swing.JOptionPane.showConfirmDialog( this,"Esta seguro de salir?",
                "Confirmar",javax.swing.JOptionPane.YES_NO_OPTION );
        if(res==javax.swing.JOptionPane.YES_OPTION){
            this.dispose();
        }
}//GEN-LAST:event_jbCancelarActionPerformed

    private void jbCancelarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbCancelarKeyPressed

        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jbCancelar.doClick();
        }
}//GEN-LAST:event_jbCancelarKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JButton jbAceptar;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JFormattedTextField jftfCantidad;
    private javax.swing.JTextField jtfCodSuministro;
    private javax.swing.JTextField jtfDescSuministro;
    private javax.swing.JTextField jtfProveedor;
    private javax.swing.JTextField jtfRuc;
    private javax.swing.JLabel lblProveedor;
    // End of variables declaration//GEN-END:variables

}
